package com.lz.fruitday.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lz.fruitday.R;
import com.lz.fruitday.adapter.FoodPagerAdapter;
import com.lz.fruitday.widget.TitleBar;

import java.util.ArrayList;



/**
 * A simple {@link Fragment} subclass.
 */
public class FoodFragment extends Fragment implements ViewPager.OnPageChangeListener,RadioGroup.OnCheckedChangeListener{

    @ViewInject(R.id.vp_food)
    private ViewPager vpFood;
    @ViewInject(R.id.food_rg)
    private RadioGroup rgFood;
    private ArrayList<Fragment> fragments=new ArrayList<>();
    private FoodPagerAdapter foodAdapter;


    private RadioGroup radioGroup;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_food, container, false);
        ViewUtils.inject(this, view);
        initView();
        foodAdapter=new FoodPagerAdapter(getChildFragmentManager(),fragments);
        vpFood.setAdapter(foodAdapter);
        return view;
    }

    private void initView() {

       /* TitleBar titleBar = (TitleBar) getActivity().getActionBar().getCustomView();
        radioGroup = titleBar.getBarRg();
        vpFood.setOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        RadioButton button= (RadioButton) radioGroup.getChildAt(0);
        button.setChecked(true);*/

        vpFood.setOnPageChangeListener(this);
        rgFood.setOnCheckedChangeListener(this);
        RadioButton button= (RadioButton) rgFood.getChildAt(0);
        button.setChecked(true);
        fragments.add(new BaiKeFragment());
        fragments.add(new FriendsFragment());
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.food_baike_btn:
                vpFood.setCurrentItem(0);
                break;
            case R.id.food_friend_btn:
                vpFood.setCurrentItem(1);
                break;

          /*  case R.id.left_btn:
                vpFood.setCurrentItem(0);
                break;
            case R.id.right_btn:
                vpFood.setCurrentItem(1);
                break;*/
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton button= (RadioButton) rgFood.getChildAt(position);
       // RadioButton button= (RadioButton) radioGroup.getChildAt(position);
        button.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
